<template>
  <div class="wrapper">
    <img
      src="http://www.dell-lee.com/imgs/vue3/user.png"
      alt=""
      class="wrapper__img"
    />
    <form>
      <div class="wrapper__input">
        <input
          type="text"
          placeholder="请输入用户名"
          class="wrapper__input__content"
          v-model="username"
          autocomplete
        />
      </div>
      <div class="wrapper__input">
        <input
          type="password"
          placeholder="请输入密码"
          class="wrapper__input__content"
          v-model="password"
          autocomplete
        />
      </div>
      <div class="wrapper__input">
        <input
          type="password"
          placeholder="确认密码"
          class="wrapper__input__content"
          v-model="ensurement"
          autocomplete
        />
      </div>
      <div class="wrapper__login-button" @click="handleRegister">注册</div>
      <div class="wrapper__login-link" @click="goLogin">已有账号去登录</div>
    </form>
    <Toast v-if="show" :message="toastMessage" />
  </div>
</template>
<script>
import { reactive, toRefs } from '@vue/reactivity';
import { useRouter } from 'vue-router';
import { post } from '@/utils/request';
import Toast, { useToastEffect } from '@/components/Toast.vue';

// 处理注册逻辑
const useRegisterEffect = (showToast) => {
  const router = useRouter();
  const data = reactive({ username: '', password: '', ensurement: '' });
  const { username, password, ensurement } = toRefs(data);
  const handleRegister = async () => {
    try {
      const result = await post('/api/user/register', {
        username: data.username,
        password: data.password,
      });
      if (result?.errno === 0) {
        showToast('注册成功');
        router.push({ name: 'Login' });
      } else {
        showToast('注册失败');
      }
    } catch (error) {
      showToast('请求失败');
    }
  };
  return { username, password, ensurement, handleRegister };
};
// 跳转登录页逻辑
const goLoginEffect = () => {
  const router = useRouter();
  const goLogin = () => {
    router.push('/login');
  };
  return { goLogin };
};
export default {
  name: 'Register',
  components: { Toast },
  setup() {
    const { goLogin } = goLoginEffect();
    const { show, toastMessage, showToast } = useToastEffect();
    const { username, password, ensurement, handleRegister } =
      useRegisterEffect(showToast);
    return {
      goLogin,
      handleRegister,
      username,
      password,
      ensurement,
      show,
      toastMessage,
    };
  },
};
</script>
<style lang="scss" scoped>
@import '@/style/viriables.scss';
@import '@/style/mixins.scss';
.wrapper {
  overflow-y: auto;
  @include fix-content;
  background: $darkBgColor;
  &__img {
    display: block;
    margin: 0 auto 0.4rem;
    width: 0.66rem;
    height: 0.66rem;
  }
  &__input {
    height: 0.48rem;
    margin: 0 0.4rem 0.16rem 0.4rem;
    padding: 0 0.16rem;
    background: #f9f9f9;
    border: 0.01rem solid rgba(0, 0, 0, 0.1);
    border-radius: 0.6rem;
    border-radius: 0.6rem;
    &__content {
      line-height: 0.48rem;
      border: none;
      outline: none;
      width: 100%;
      background: none;
      font-size: 0.16rem;
      color: $content-notice-fontcolor;
      &::placeholder {
        color: $content-notice-fontcolor;
      }
    }
  }
  &__login-button {
    margin: 0.32rem 0.4rem 0.16rem 0.4rem;
    line-height: 0.48rem;
    background: #0091ff;
    box-shadow: 0 0.04rem 0.08rem 0 rgba(0, 145, 255, 0.32);
    border-radius: 0.04rem;
    border-radius: 0.04rem;
    color: $bgColor;
    font-size: 0.16rem;
    text-align: center;
  }
  &__login-link {
    text-align: center;
    font-size: 0.14rem;
    color: $content-notice-fontcolor;
  }
}
</style>